package com.spark.colliery.module.mine.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.spark.colliery.MainActivity
import com.spark.colliery.R
import com.spark.common.BaseActivity
import com.spark.common.Const
import com.spark.colliery.common.ext.*
import com.spark.common.model.UserModel
import com.spark.common.net.Http
import com.spark.colliery.module.mine.ForgetPasswordActivity
import com.spark.common.ext.*
import kotlinx.android.synthetic.main.act_login.*

/**
 * login
 */
class LoginActivity : BaseActivity() {
    companion object {
        fun actionStart(host: Activity) {
            val intent = Intent(host, LoginActivity::class.java)
            host.startActivity(intent)
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_login
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // hide&show qr_password
        checkbox_password_switch.setOnCheckedChangeListener { _, isChecked ->
            et_password.password(!isChecked)
        }
        // forgetpassword
        tv_forget_password.setOnClickListener { ForgetPasswordActivity.actionStart(this) }
        // login
        btn_login.setOnClickListener {
            val phone = et_phone.text.toString().trim()
            val password = et_password.text.toString().trim()
            if (phone.isNullOrBlank()) {
                toast("请输入手机号")
                return@setOnClickListener
            }
            if (password.isNullOrBlank()) {
                toast("请输入密码")
                return@setOnClickListener
            }
            hideIm()
            login(phone, password)
        }
    }

    @SuppressLint("CheckResult")
    private fun login(account: String, password: String) {
        /*Single.create<Boolean> { it.onSuccess(true) }
                .wrapIO()
                .subscribe(
                        {
                            *//*EMClient.getInstance().chatManager().loadAllConversations()
                            EMClient.getInstance().groupManager().loadAllGroups()*//*

                            SPUtils.getInstance().put("account", account)
                            SPUtils.getInstance().put("qr_password", EncryptUtils.encryptMD5ToString(qr_password, "sha"))

                            toast("登陆成功")

                            MainActivity.actionStartBack(this)
                            finish()
                        },
                        {
                            it.printStackTrace()
                        }
                )*/
        val params = mutableMapOf(
                Pair(Const.qr_username, account),
                Pair(Const.qr_password, password),
                Pair(Const.qr_mobile, "1")
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_login}", params)
                .wrapIO()
                .subscribe(
                        {
                            LogUtils.e(it)
                            Gson().fromJson(it, UserModel::class.java)?.apply {
                                if (errno == 0) {
                                    success("登陆成功")
                                    Const.saveToken(token)
                                    Const.saveUser(it)
                                    MainActivity.actionStart(this@LoginActivity)
                                    finish()
                                } else {
                                    error(message)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                             MainActivity.actionStart(this@LoginActivity)
                            LogUtils.e("登录失败")
                            error("登录失败")
                            hideLoader()
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading("登陆中...")
                        }
                )
    }
}
